<?php
$db=new PDO('mysql:host=localhost;dbname=db2', 'root', '12qwas');
$ps=$db->prepare("select * from users limit ?");
$ps->bindValue(1, 2, PDO::PARAM_INT); //第1个参数的值是2，整型数据
$ps->execute();//执行查询
foreach ($ps as $record){
    echo $record['name'],"\t",$record['age'],"\n";
}
echo "------------跳过1条记录，取其后的2条记录--------------\n";
$ps=$db->prepare('select * from users limit :offset, :length'); //使用命名占位符，以冒号:开始
$ps->bindValue(':offset',1,PDO::PARAM_INT); //设置绑定占位符的值及类型
$ps->bindValue(':length',2,PDO::PARAM_INT);
$ps->execute();//执行语句，这里就不用传递数组参数了
foreach ($ps as $record){
    echo $record['name'],"\t",$record['age'],"\n";
}
echo "------------绑定变量到占位符--------------\n";
$ps=$db->prepare('select * from users limit :offset, :length');
$ps->bindParam(':offset',$offset,PDO::PARAM_INT);//此变量作为引用被绑定
$ps->bindParam(':length',$length,PDO::PARAM_INT);
$offset=$length=1; //跳过1条记录，取其后的1条记录
$ps->execute();//只在 PDOStatement::execute()被调用的时候才取绑定变量的值。
foreach ($ps as $record){
    echo $record['name'],"\t",$record['age'],"\n";
}
echo "------------绑定变量到结果集的某列--------------\n";
$ps=$db->prepare('select * from users limit 1,2');
$ps->bindColumn(1,$name);//通过列号绑定，从1开始，默认字符串类型
$ps->bindColumn('age', $age, PDO::PARAM_INT);//通过列名绑定
$ps->execute();
foreach ($ps as $record){
    echo "{$name}\t{$age}\n";//可直接使用绑定到列的变量名
}
